-- Language Server Protocol

return {
  {
    "mason-org/mason.nvim",
    cmd = "Mason",
    keys = { { "<leader>pm", "<cmd>Mason<cr>", desc = "Mason" } },
    build = ":MasonUpdate",
    opts_extend = { "ensure_installed" },
    opts = {
      ensure_installed = {
        -- LSP
        "bash-language-server",            -- bash, csh, ksh, sh, zsh
        "python-lsp-server",               -- python
        "yaml-language-server",            -- yaml
        "vue-language-server",             -- vue
        "clangd",                          -- c, c++
        "docker-compose-language-service", -- dockercompose
        "dockerfile-language-server",      -- dockerfile
        "json-lsp",                        -- json
        "marksman",                        -- markdown
        "stylua",                          -- lua, luau
        "html-lsp",                        -- html
        "django-template-lsp",             -- html
        "eslint-lsp",                      -- javascript, typescript
        "css-lsp",                         -- css, scss, less
        "vim-language-server",             -- vimls (vimscript)


        -- Linter
        -- "flake8",
        "markdownlint-cli2",               -- markdown
        "shellcheck",                      -- bash
        "pylint",                          -- python
        "luacheck",                        -- lua
        "jsonlint",                        -- json
        "eslint_d",                        -- javascript, typescript
        "htmlhint",                        -- html
        "stylelint",                       -- css, scss, less
        "cpplint",                         -- c, c++
        "vint",                            -- vimscript

        -- Format
        "markdownlint-cli2",               --markdown
        "shfmt",                           -- bash
        "stylua",                          -- lua
        "pyink",                           -- python
        "prettier",                        -- angular, css, flow, graphql, html, json, jsx, javascript, less, markdown, scss, typescript, vue, yaml)
        "clang-format",                    -- c, c#, c++, json, java, javascript
      },
    },
  },

  {
    "neovim/nvim-lspconfig",
    event = "LazyFile",
    opts = {
      servers = {
        ['*'] = {
          keys = {
            -- Disable a keymap
            { "K", false },
          },
        },
      },
    },
    dependencies = {
      "mason.nvim",
      {
        "mason-org/mason-lspconfig.nvim",
        config = function()
          vim.keymap.set("n", "gh", vim.lsp.buf.hover,           { desc = "显示提示文档" })
          vim.keymap.set("n", "gd", vim.lsp.buf.definition,      { desc = "跳转到函数定义" })
          vim.keymap.set("n", "gD", vim.lsp.buf.declaration,     { desc = "跳转到函数声明" })
          vim.keymap.set("n", "go", vim.lsp.buf.type_definition, { desc = "跳转到变量类型定义的位置" })
          vim.keymap.set("n", "gr", vim.lsp.buf.references,      { desc = "跳转到引用了对应变量或函数的位置" })
          vim.keymap.set("n", "gx", vim.lsp.buf.code_action,     { desc = "在出现警告或错误的地方打开建议的修复方法" } )
          vim.keymap.set("n", "gX", vim.diagnostic.open_float,   { desc = "浮动窗口显示所在行警告或错误信息" })
          vim.keymap.set("n", "<leader>rn", vim.lsp.buf.rename,  { desc = "变量重命名" })
          vim.keymap.set("n", "<leader>rf", function() Snacks.rename.rename_file() end, { desc = "文件重命名" })
          vim.keymap.set("n", "gl", function() Snacks.picker.lsp_config() end, { desc = "Lsp Info" })
          -- vim.keymap.set({ "n", "x" }, "<leader>f", function()
          -- vim.lsp.buf.format({ async = true })
          -- end, opts) -- <space>f进行代码格式化
          local ret = {
            -- options for vim.diagnostic.config()
            ---@type vim.diagnostic.Opts
            diagnostics = {
              underline = true,
              update_in_insert = false,
              virtual_text = {
                spacing = 4,
                source = "if_many",
                prefix = "●",
                -- this will set set the prefix to a function that returns the diagnostics icon based on the severity
                -- prefix = "icons",
              },
              severity_sort = true,
              signs = {
                text = {
                  [vim.diagnostic.severity.ERROR] = LazyVim.config.icons.diagnostics.Error,
                  [vim.diagnostic.severity.WARN]  = LazyVim.config.icons.diagnostics.Warn,
                  [vim.diagnostic.severity.HINT]  = LazyVim.config.icons.diagnostics.Hint,
                  [vim.diagnostic.severity.INFO]  = LazyVim.config.icons.diagnostics.Info,
                },
              },
            },
          }
          return ret
        end,
      },
    },
  },
}
